﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MutiCodecO.codeclib
{
    class Asii2Hex : ICodec
    {
        private char IntToHex(int n)
        {
            if (n <= 9)
            {
                return (char)((ushort)(n + 0x30));
            }
            return (char)((ushort)((n - 10) + 0x41));
        }

        public String parse(String strInput)
        {
            byte[] bytesInput = Encoding.Default.GetBytes(strInput);
            StringBuilder strRslt = new StringBuilder();
            for (int i = 0; i < bytesInput.Length; i++)
            {
                strRslt.Append(IntToHex(((int)bytesInput[i]) / 16));
                strRslt.Append(IntToHex(((int)bytesInput[i]) % 16));
                if (i != bytesInput.Length - 1)
                    strRslt.Append(',');
            }
            return strRslt.ToString();
        }

        public String parse(String strInput, String prefix, String partion)
        {
            byte[] bytesInput = Encoding.Default.GetBytes(strInput);
            StringBuilder strRslt = new StringBuilder();
            for (int i = 0; i < bytesInput.Length; i++)
            {
                strRslt.Append(prefix);
                strRslt.Append(IntToHex(((int)bytesInput[i]) / 16));
                strRslt.Append(IntToHex(((int)bytesInput[i]) % 16));
                if (i != bytesInput.Length - 1)
                    strRslt.Append(partion);
            }
            return strRslt.ToString();
        }
    }
}
